package com.wonderful.wedding.client;

import com.google.common.base.Strings;
import com.wonderful.wedding.model.MyCloudResult;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("unchecked")
public class MyCloudClient {

    protected HttpClient mClient;

    public MyCloudClient() {
        RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(5 * 1000)
            .setExpectContinueEnabled(false).build();

        mClient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig)
            .build();
    }

    public MyCloudResult createPerson(String url, Map<String, String> header, Map<String, Object> body, File file) throws UnsupportedEncodingException, IOException {
        HttpPost httpPost = new HttpPost(url);
        if (header != null) {
            for (String key : header.keySet()) {
                httpPost.setHeader(key, header.get(key));
            }
        }
        httpPost.setEntity(generateMultipartEntity(body, file));

        String response = EntityUtils.toString(mClient.execute(httpPost).getEntity(), "UTF-8");

        return parseToResult(response);
    }

    public MyCloudResult addFace(String url, HashMap<String, String> header, Map<String, Object> body, File image) throws IOException {
        HttpPost httpPost = new HttpPost(url);
        if (header != null) {
            for (String key : header.keySet()) {
                httpPost.setHeader(key, header.get(key));
            }
        }

        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();

        if (body != null) {
            for (String key : body.keySet()) {
                multipartEntityBuilder.addTextBody(key, body.get(key).toString());
            }
        }

        FileBody fileBody = new FileBody(image, ContentType.create("image/jpeg"));
        multipartEntityBuilder.addPart("images[0]", fileBody);

        httpPost.setEntity(multipartEntityBuilder.build());

        String response = EntityUtils.toString(mClient.execute(httpPost).getEntity(), "UTF-8");

        return parseToResult(response);
    }

    private HttpEntity generateMultipartEntity(Map<String, Object> body, File file) throws UnsupportedEncodingException {
        MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();

        if (body != null) {
            for (String key : body.keySet()) {
                multipartEntityBuilder.addTextBody(key, body.get(key).toString());
            }
        }

        FileBody fileBody = new FileBody(file, ContentType.create("image/jpeg"));
        multipartEntityBuilder.addPart("image", fileBody);
        return multipartEntityBuilder.build();
    }

    private MyCloudResult parseToResult(String response) {
        if (Strings.isNullOrEmpty(response)) {
            return new MyCloudResult(Integer.MIN_VALUE, "");
        } else {
            JSONObject jsonObject = new JSONObject(response);
            return new MyCloudResult(jsonObject.getInt("code"), jsonObject.getJSONObject("data"), jsonObject.getString("message"));
        }
    }
}
